package com.example.demo.test;


import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;

/**
 * @description
 *
 * @author lilinchun
 * @date 2023/09/07
 */
public class demo3 {


    public static void main(String[] args) {
        try {
            // 读取图像
            BufferedImage image = ImageIO.read(new File("C:\\Users\\Administrator\\Desktop\\ebc\\4610.jpg"));

            // 锐化图像
            BufferedImage sharpened = sharpenImage(image);

            // 保存锐化后的图像
            ImageIO.write(sharpened, "jpg", new File("C:\\Users\\Administrator\\Desktop\\ebc\\output1.jpg"));

        } catch (Exception e) {


        }
    }

    public static BufferedImage sharpenImage(BufferedImage image) {
        int width = image.getWidth();
        int height = image.getHeight();
        BufferedImage sharpened = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

        int[][] filter = {{0, -1, 0}, {-1, 5, -1}, {0, -1, 0}};

        for (int y = 1; y < height - 1; y++) {
            for (int x = 1; x < width - 1; x++) {
                int[] rgbValues = new int[3];

                for (int i = -1; i <= 1; i++) {
                    for (int j = -1; j <= 1; j++) {
                        Color color = new Color(image.getRGB(x + i, y + j));
                        int red = color.getRed();
                        int green = color.getGreen();
                        int blue = color.getBlue();

                        rgbValues[0] += red * filter[i + 1][j + 1];
                        rgbValues[1] += green * filter[i + 1][j + 1];
                        rgbValues[2] += blue * filter[i + 1][j + 1];
                    }
                }

                int newRed = Math.min(Math.max(rgbValues[0], 0), 255);
                int newGreen = Math.min(Math.max(rgbValues[1], 0), 255);
                int newBlue = Math.min(Math.max(rgbValues[2], 0), 255);

                Color newColor = new Color(newRed, newGreen, newBlue);
                sharpened.setRGB(x, y, newColor.getRGB());
            }
        }

        return sharpened;
    }


}
